/*              Copyright(c) 2008 Dave Sexton              *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace DaveSexton.Sandcastle
{
	internal sealed partial class XmlUrlMappingProviderEditor : UserControl
	{
		#region Public Properties
		public string Configuration
		{
			get
			{
				return ReadTable();
			}
			set
			{
				urlsBindingSource.SuspendBinding();

				try
				{
					table.Clear();

					FillTable(value);

					table.AcceptChanges();
				}
				finally
				{
					urlsBindingSource.ResumeBinding();
				}
			}
		}
		#endregion

		#region Private / Protected
		private readonly DataTable table = CreateDataTable();
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="XmlUrlMappingProviderEditor" /> class.
		/// </summary>
		public XmlUrlMappingProviderEditor()
		{
			InitializeComponent();

			urlsBindingSource.DataSource = table;
		}
		#endregion

		#region Methods
		private static DataTable CreateDataTable()
		{
			DataTable table = new DataTable();
			table.Locale = System.Globalization.CultureInfo.CurrentCulture;

			DataColumn id = table.Columns.Add("id", typeof(string));
			table.PrimaryKey = new DataColumn[] { id };

			table.Columns.Add("href", typeof(Uri));

			return table;
		}

		private string ReadTable()
		{
			urlDataGridView.EndEdit();

			StringBuilder output = new StringBuilder(1024);

			System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings();
			settings.Indent = true;
			settings.ConformanceLevel = System.Xml.ConformanceLevel.Fragment;

			using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(output, settings))
			{
				foreach (DataRow url in table.Rows)
				{
					if (url.RowState == DataRowState.Deleted)
						continue;

					writer.WriteStartElement("url");
					writer.WriteAttributeString("id", (string) url["id"]);

					Uri href = (url.IsNull("href")) ? null : (Uri) url["href"];

					if (href != null)
						writer.WriteAttributeString("href", href.ToString());

					writer.WriteEndElement();
				}
			}

			return output.ToString();
		}

		private void FillTable(string configuration)
		{
			if (!string.IsNullOrEmpty(configuration))
			{
				using (System.IO.StringReader input = new System.IO.StringReader(configuration))
				{
					System.Xml.XmlReaderSettings settings = new System.Xml.XmlReaderSettings();
					settings.IgnoreComments = true;
					settings.IgnoreProcessingInstructions = true;
					settings.IgnoreWhitespace = true;
					settings.ConformanceLevel = System.Xml.ConformanceLevel.Fragment;

					using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(input, settings))
					{
						while (reader.ReadToFollowing("url"))
						{
							if (reader.Depth == 0)
							{
								string id = reader.GetAttribute("id");
								string href = reader.GetAttribute("href");

								DataRow row = table.NewRow();
								row["id"] = id;

								if (!string.IsNullOrEmpty(href))
									row["href"] = new Uri(href, UriKind.RelativeOrAbsolute);

								table.Rows.Add(row);
							}
						}
					}
				}
			}
		}
		#endregion
	}
}
